package hashchainutil.common;

import com.tyqx.hashchainbean.vo.PerOrResourceParamVO;
import com.tyqx.hashchainbean.vo.PerOrResourceResultVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TreeNode {

    //存放转换后数据的集合
//    private List<Map<String,Object>> comboTreeList=new ArrayList<Map<String,Object>>();

    /**
     * 将角色封装成树开始
     *
     * @param list
     * @param fid  父id
     */
    public static List<Map<String, Object>> createComboTreeTree(List<PerOrResourceResultVO> list, List<PerOrResourceParamVO> rpList, Long fid) {
        List<Map<String, Object>> comboTreeList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = null;
            PerOrResourceResultVO sysPermission = (PerOrResourceResultVO) list.get(i);
            if (sysPermission.getParentId().equals(fid)) {
                map = new HashMap<String, Object>();
                //这里必须要将对象角色的id、name转换成ComboTree在页面的显示形式id、text
                //ComboTree,不是数据表格，没有在页面通过columns转换数据的属性
                map.put("id", list.get(i).getId());         //id
                map.put("text", list.get(i).getName());      //权限名
                map.put("state", "open");
                for (int k = 0; k < rpList.size(); k++) {
                    if (rpList.get(k).getPerOrRrId() == list.get(i).getId()) {
                        map.put("checked", "true");
                    }
                }
                map.put("children", createComboTreeChildren(list, rpList, sysPermission.getId()));
            }
            if (map != null)
                comboTreeList.add(map);
        }
        return comboTreeList;
    }


    /**
     * 递归设置role树
     *
     * @param list
     * @param fid
     * @return
     */
    public static List<Map<String, Object>> createComboTreeChildren(List<PerOrResourceResultVO> list, List<PerOrResourceParamVO> rpList, Long fid) {
        List<Map<String, Object>> childList = new ArrayList<Map<String, Object>>();
        for (int j = 0; j < list.size(); j++) {
            Map<String, Object> map = null;
            PerOrResourceResultVO treeChild = (PerOrResourceResultVO) list.get(j);
            if (treeChild.getParentId().equals(fid)) {
                map = new HashMap<String, Object>();
                //这里必须要将对象角色的id、name转换成ComboTree在页面的显示形式id、text
                //ComboTree,不是数据表格，没有在页面通过columns转换数据的属性
                map.put("id", list.get(j).getId());
                map.put("text", list.get(j).getName());
                map.put("state", "open");
                for (int k = 0; k < rpList.size(); k++) {
                    if (rpList.get(k).getPerOrRrId() == list.get(j).getId()) {
                        map.put("checked", "true");
                    }
                }
                map.put("children", createComboTreeChildren(list, rpList, treeChild.getId()));
            }
            if (map != null)
                childList.add(map);
        }
        return childList;
    }
}
